//
//  ScanQRCodeViewController.m
//  二维码综合案例
//
//  Created by Guo Rui on 2017/10/23.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "ScanQRCodeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "QRCodeTool.h"

@interface ScanQRCodeViewController ()<QRCodeToolScanDelegate>
@property (weak, nonatomic) IBOutlet UIView *scanBackView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scanOverImageTopConstraint;
@property (weak, nonatomic) IBOutlet UIImageView *scanOverImageView;
@property (strong, nonatomic) AVCaptureSession *session;
@property (weak, nonatomic) AVCaptureVideoPreviewLayer *layer;

@property (strong, nonatomic) QRCodeTool *qrCodeTool;
@end

@implementation ScanQRCodeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.qrCodeTool = [[QRCodeTool alloc] init];
    
    [self.qrCodeTool scanQRCodeWithContainerView:self.view isDrawFrame:YES delegate:self];
    [self.qrCodeTool setRectOfInterest:self.scanBackView.frame];
}

#pragma mark - QRCodeToolScanDelegate
- (void)qrCodeTool:(QRCodeTool *)qrCodeTool didEncountError:(NSError *)error
{
    if (error && error.code == QRCodeErrorCodeScanNoAuthorization) {
        NSString *message = [NSString stringWithFormat:@"请去设置 -> 隐私 -> 相机 -> %@中打开按钮", [[NSBundle mainBundle].infoDictionary objectForKey:  ((__bridge NSString *)kCFBundleNameKey)]];
        UIAlertController *altController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:altController animated:YES completion:nil];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [altController addAction:action];
    }
}

- (void)qrCodeTool:(QRCodeTool *)qrCodeTool didScanResult:(NSArray<NSString *> *)codeContents
{
    NSLog(@"%@", codeContents);
    [qrCodeTool stopScanning];
    
    
    // code...
    
    UIAlertController *altController = [UIAlertController alertControllerWithTitle:@"" message:codeContents.description preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:altController animated:YES completion:nil];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"done" style:UIAlertActionStyleDefault handler:nil];
    [altController addAction:action];
}

- (void)startScanAnimation
{
    self.scanOverImageTopConstraint.constant = -CGRectGetHeight(self.scanBackView.frame);
    [self.view layoutIfNeeded];
    
    self.scanOverImageTopConstraint.constant = CGRectGetHeight(self.scanBackView.frame);
    [UIView animateWithDuration:1.f animations:^{
        [UIView setAnimationRepeatCount:MAXFLOAT];
        [self.view layoutIfNeeded];
    }];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self startScanAnimation];
    
    [self.qrCodeTool startScanning];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.qrCodeTool stopScanning];
}

- (void)stopScanAnimation
{
    [self.scanOverImageView.layer removeAllAnimations];
}



@end
